<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * SAEF Model
 * 
 * @package PyroCMS
 * @subpackage SAEF Module
 * @category Modules
 * @author Rick Holmes
 * @version 1.0 
 */
class Saef_m extends MY_Model {
	
	function __construct()
	{
		parent::__construct();		
	}
	
	/**
	 * Check to see if SAEF is enabled
	 * called by widget definition controller, since disabling a module does not disable its widgets 	 
	 *
	 * @access public	  
	 * @param void
	 * @return bool
	 */
	public function is_enabled()
	{
		$this->db->select('enabled')
			->where('slug', 'saef');
		
		$query = $this->db->get('modules');	
		return (bool) $query->row()->enabled;
	}	 	 	 	 	 	
	
	/**
	 * get the user moderation setting 
	 *  
	 * @param void
	 * @return bool
	 */
	public function requires_moderation()
	{
		$query = $this->db->get('saef_settings');
		return ($query->row() ? $query->row()->require_moderation : 0);
	}	 	 	 	 	
	
	/**
	 * Check to see if editing allowed
	 * 
	 * @access public
	 * @param void
	 * @return bool
	 */
	public function editable()
	{
		$query = $this->db->get('saef_settings');
		return ($query->row() ? $query->row()->require_moderation : 0);
	}	 	 	 	 	 	
	
	/**
	 * get the "editable wrapper"
	 * 
	 * @access public
	 * @param void
	 * @return string
	 */
	public function get_editable_wrap()
	{
		$query = $this->db->get('saef_settings');
		return ($query->row() ? $query->row()->editable_div : '');
	}	 	 	 	 	 	
	
	/**
	 * get all the settings
	 * 
	 * @access public
	 * @param void
	 * @return obj
	 */
	public function get_saef_settings()
	{
		$settings = $this->db->get('saef_settings')->row();
		
		// add admins to allowed users
		$groups = array('1');
		$groups = array_merge($groups, explode(',', $settings->allowed_user_groups));
		$settings->allowed_user_groups = $groups;
		
		return $settings;
	}	 	 	 	 	 	
	
	/**
	 * Get a list of all the categories
	 * called by Admin controller
	 * 	 	 
	 * @access public
	 * @param void
	 * @return array(obj)|bool FALSE
	 */
	public function get_categories()
	{	
		$this->db->order_by('title', 'ASC');
		$query = $this->db->get('blog_categories');
		if ($query->num_rows() > 0)
		{
			foreach ($query->result() as $row)
			{
				$categories[] = $row;
			}
			return $categories;
		}
		return FALSE;
	}
	
	/**
	 * Get a list of all categories allowed for saef
	 * called by Admin controller, widget controller	 
	 *
	 * @access public	  
	 * @param void 
	 * @return array(obj)|bool FALSE
	 */
	public function get_allowed_categories()
	{
		// check to make sure nobody deleted a category
		$this->_sync_categories();
		
		$this->db->select('saef_categories.cat_id, blog_categories.title')
			->from('saef_categories')
			->join('blog_categories', 'blog_categories.id=saef_categories.cat_id');
			
		$query = $this->db->get(); 
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $row)
			{
				$allowed[] = $row;
			}
			
			return $allowed;
		}
		
		return FALSE;
	}
	
	/**
	 * Get a list of just the category ids allowed
	 * called by Admin controller	 
	 *
	 * @access public	  
	 * @param void
	 * @return array(int)|bool FALSE
	 */
	public function get_allowed_category_ids()
	{
		// check to make sure nobody deleted a category
		$this->_sync_categories();
		
		$this->db->select('cat_id');
		$query = $this->db->get('saef_categories');
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $row)
			{
				$allowed[] = $row->cat_id;
			}
			return $allowed;
		}
		return FALSE;
	}
	
	/**
	 * Get the allowed user group ids
	 * called by widget controller	 
	 *
	 * @access public	  
	 * @param void
	 * @return array(int)
	 */
	public function get_allowed_group_ids()
	{
		// check to make sure nobody deleted a group
		$this->_sync_groups();
		
		$this->db->select('allowed_user_groups');
		$query = $this->db->get('saef_settings');
		
		$users = array(1); // admins always allowed, may already be in db, but doesn't matter
		if ($query->num_rows > 0)
		{
			$str = $query->row()->allowed_user_groups;
			
			if (strpos($str, ',') !== FALSE)
			{
				$arr = explode(',', $str);
				$users = array_merge($users, $arr);
			}
			else
			{
				array_push($users, $str);
			}
		}
		return $users;
	}	  	 	 	
	
	/**
	 * Set the allowed categories
	 * called by Admin controller	 
	 *
	 * @access public	  
	 * @param array
	 * @uses $this->get_allowed_category_ids()	 
	 * @return bool
	 */
	public function set_allowed_categories($input)
	{
		// get the currently allowed in order to check existence
		$currents = $this->get_allowed_category_ids();
		
		if (!is_array($currents))
		{
			$currents = array();
		}
		
		// if in current, but not in input, delete
		if (!empty($currents))
		{
			foreach($currents as $c)
			{
				if (!in_array($c, $input))
				{
					$this->db->where('cat_id', $c)
						->delete('saef_categories');
				}
			}
		}
						
		$to_insert = array();
		foreach($input as $i)
		{
			if (!in_array($i, $currents))
			{
				$arr = array(
					'cat_id' => $i
				);
				array_push($to_insert, $arr);
			}									
		}
		
		if (!empty($to_insert))	
		{
			$this->db->insert_batch('saef_categories', $to_insert);
		}
		 
		
		if ($this->db->affected_rows() > 0)
		{
			return TRUE;
		}
		 
		return FALSE;
	}
	
	/**
	 * delete all the allowed categories
	 * only called when all checks deleted
	 * error message thrown immediately after
	 * 
	 * @access public
	 * @param void
	 * @return void
	 */
	public function delete_allowed_categories()
	{
		$this->db->truncate('saef_categories');
	}	 	 	 	 	 	 	 	
	
	/**
	 * Update the saef settings table
	 *
	 * @access public	  
	 * @param array
	 * @return bool
	 */
	public function update_settings($settings)
	{
		$args = array(
			'require_moderation' => $settings['require_moderation'],
			'allowed_user_groups' => @implode(',', $settings['user_groups']),
			'allow_editing' => $settings['allow_editing'],
			'admin_edit' => $settings['admin_edit'],
			'editable_div' => $settings['editable_div'],
		);
		
		// both fields have defaults, so can simply update
		$this->db->update('saef_settings', $args);
		return TRUE;
	}	 	 	 	 	
	
	/**
	 * simple check for existence of title only
	 * called by widget controller (Ajax form handler) for SAEF form validation	 
	 *
	 * @access public	  
	 * @param string
	 * @return bool
	 */
	public function check_title($title)
	{
		$params = array('title' => $title);
		return parent::count_by($params) == 0;
	}	 	 	 	 		
	
	/**
	 * get an admin's (first one found) details for moderation email
	 * called by widget controller (Ajax form handler)	 
	 *
	 * @access public	  
	 * @param void
	 * @return obj
	 */
	public function get_admin_data()
	{
		$this->db->select('display_name, first_name, last_name, email')
			->from('users')
			->join('profiles', 'profiles.user_id=users.id')
			->where('users.group_id', 1)
			->limit(1);
		$query = $this->db->get();
		return $query->row();	
	}
	
	/**
	 * get just the category name for email
	 * called by widget controller (Ajax form handler)		 
	 *
	 * @access public	  
	 * @param int
	 * @return string
	 */
	public function get_category_title($id)
	{
		$this->db->select('title')
			->where('id', $id);
		$query = $this->db->get('blog_categories');
		return $query->row()->title;
	}
	
	/**
	 * get the author of a post
	 * 
	 * @access public
	 * @param string
	 * @return int
	 */	 	 	 	 	 	
	public function get_entry_author($slug)
	{
		$query = $this->db->select('author_id')
			->from('blog')
			->where('slug', $slug)
			->get();
		if ($query->num_rows() > 0)
		{
			return $query->row()->author_id;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * update blog content
	 * 
	 * @access public
	 * @param string
	 * @param string
	 * @param string	 	  
	 * @return bool
	 */
	public function update_blog_content($slug, $area, $content)
	{
		//$q = "UPDATE `default_blog` SET `" . $area . "` = '" . $content . "' WHERE `slug` = '" . $slug . "';";
		//$this->db->query($q);
		$this->db->where('slug', $slug)
			->update('blog', array($area => $content));
		return (bool) $this->db->affected_rows();
	}	 	 	 	 	 	
	
	/**
	 * synchronize the allowed categories and the blog categories
	 * in case somebody deleted a category used by SAEF
	 * 
	 * @access privat
	 * @param void
	 * @return void
	 */
	private function _sync_categories()
	{
		// get the blog categories
		$blog_cats = array();
		$this->db->select('id');
		$query = $this->db->get('blog_categories');
		if ($query->num_rows > 0)
		{
			foreach($query->result() as $row)
			{
				$blog_cats[] = $row->id;
			}
		}
		
		// get the allowed categories
		$allowed_cats = array();
		$this->db->select('cat_id');
		$query = $this->db->get('saef_categories');
		if ($query->num_rows() > 0)
		{
			foreach($query->result() as $row)
			{
				$allowed_cats[] = $row->cat_id;	
			}
		}
		
		$to_delete = array();
		
		foreach($allowed_cats as $cat)
		{
			if (!in_array($cat, $blog_cats))
			{
				array_push($to_delete, $cat);
			}
		}
		
		if (!empty($to_delete))
		{
			$this->db->where_in('cat_id', $to_delete)
				->delete('saef_categories');
		}
	}	 	 	 	 	 	 	
	
	/**
	 * synchronize the allowed user groups and the user groups
	 * in case somebody deleted a user group used by SAEF
	 * 
	 * @access private
	 * @param void
	 * @return void
	 */
	private function _sync_groups()
	{
		// get the allowed groups
		$allowed_groups = array();
		$this->db->select('allowed_user_groups');
		$query = $this->db->get('saef_settings');
		if ($query->num_rows > 0)
		{
			$groups_str = $query->row()->allowed_user_groups;
			$allowed_groups = @explode(',', $groups_str);
		}
		else
		{
			// none set yet, no need to go further
			return;
		}
		
		// get all the groups
		$groups = array();
		$this->db->select('id');
		$query = $this->db->get('groups');
		foreach($query->result() as $row) // always something here
		{
			$groups[] = $row->id;
		}	
		
		$to_update = array_intersect($allowed_groups, $groups);
		$update_str = @implode(',', $to_update);
		$this->db->update('saef_settings', array('allowed_user_groups'=>$update_str));
	}	 	 	 	 	 	 	
		 	 	 	 		 	 	 	 		 	 	 	 	 	 	 		 	 	 	 		 	 	 	 		 	 	 	 		
}      

/* end of file saef_m.php */
/* location /addons/shared_addons/modules/saef/models/saef_m.php */